#include <iostream>
#include "Worker.h"

using namespace std;

WorkerImp::WorkerImp(EventProcessor& _master, vector<int> _work)
#ifdef DEBUG_EVPROC
: EventProcessorImp(true, "Worker") // comment to remove debug
#endif
{
  	Master.CopyFrom(_master);
	work.assign(_work.begin(),_work.end());		
  	RegisterMessageProcessor(DieMessage::type, &Die, 0 /*priority*/);
	SendTheSum();  	
}


void WorkerImp::SendTheSum(){
	
	vector<int>::iterator xit;	
	long int xSum = 0;
	for(xit = work.begin();xit != work.end();++xit){
		xSum += *xit;
	}

	EventProcessor myInterfClone;
	myInterfClone.CopyFrom(myInterface);	
	
	DoneMessage_Factory(Master, xSum, myInterfClone);

}

MESSAGE_HANDLER_DEFINITION_BEGIN(WorkerImp, Die, DieMessage) 

	cout<<"The worker is dead !!!"<<endl;
	EventProcessor myInterfClone;
	myInterfClone.CopyFrom(evProc.myInterface);	

	WorkerDoneMessage_Factory(evProc.Master, 0, myInterfClone);
	evProc.EventProcessorImp::processDieMessage(evProc,msg);

MESSAGE_HANDLER_DEFINITION_END
